﻿using System;
using System.Windows.Forms;
using SO_DataFetcher.PubFunctions;
using SO_DataFetcher.PubFunctions.DataLink;

namespace SO_DataFetcher.Forms
{
    public partial class FrmDbLink : Form
    {
        private readonly string _dbLinkId;
        public DataLinkStruct TempDataLink;
        private DataLinkStruct _oldDataLink;
        private readonly string _operateType;     

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="linkId"></param>
        /// <param name="type"></param>
        public FrmDbLink(string linkId, string type)
        {
            InitializeComponent();

            this._operateType = type;

            switch (_operateType)
            {
                case "Add":
                    this._dbLinkId = string.Empty;
                    this.TempDataLink = new DataLinkStruct();
                    break;
                case "Edit":
                    this._dbLinkId = linkId;
                    
                    this.TempDataLink = PubDataLinks.DataLinks[this._dbLinkId];

                    if (!PubDataLinks.DataLinks.ContainsKey(this.TempDataLink.Id))
                    {
                        MessageBox.Show(@"没有查询到对应的数据库连接，请重新连接。");
                        this.Close();
                    }
                   
                    this._oldDataLink = PubDataLinks.DataLinks[this._dbLinkId];

                    break;
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (_operateType.Equals("Edit"))
            {
                if (CheckChanged())
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(@"当前数据没有发生变化");
                    this.DialogResult = DialogResult.Cancel;
                }
            }
            else if(_operateType.Equals("Add"))
            {
                this.TempDataLink.Index = PubCommon.SetIndex();
                this.DialogResult = DialogResult.OK;
            }
        }

   

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }


        /// <summary>
        /// 判断是否已经发生变化
        /// </summary>
        /// <returns></returns>
        private bool CheckChanged()
        {
            if (!TempDataLink.Id.Equals(_oldDataLink.Id))
                return true;

            if (!TempDataLink.Ip.Equals(_oldDataLink.Ip))
                return true;

            if (!TempDataLink.Port.Equals(_oldDataLink.Port))
                return true;

            if (!TempDataLink.Name.Equals(_oldDataLink.Name))
                return true;

            if (!TempDataLink.Password.Equals(_oldDataLink.Password))
                return true;

            if (!TempDataLink.DataTable.Equals(_oldDataLink.DataTable))
                return true;

            if (!TempDataLink.Description.Equals(_oldDataLink.Description))
                return true;

            if (!TempDataLink.LinkType.Equals(_oldDataLink.LinkType))
                return true;

            return false;
        }

        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmDbLink_Load(object sender, EventArgs e)
        {
            switch (this._operateType)
            {
                case "Add":
                    this.cmbType.Text = @"Oracle";
                    this.txtIp.Text = string.Empty;
                    this.txtPort.Text = string.Empty;
                    this.txtName.Text = string.Empty;
                    this.txtPsw.Text = string.Empty;
                    this.txtDes.Text = string.Empty;
                    this.cmbDataBase.Text = string.Empty;
                    this.btnGetDataList.Text = @"获取";
                    break;
                case "Edit":
                    this.cmbType.Text = TempDataLink.LinkType;
                    this.txtIp.Text = TempDataLink.Ip;
                    this.txtPort.Text = TempDataLink.Port;
                    this.txtName.Text = TempDataLink.Name;
                    this.txtPsw.Text = TempDataLink.Password;
                    this.txtDes.Text = TempDataLink.Description;
                    this.cmbDataBase.Text = TempDataLink.DataTable;
                    this.txtId.Text = TempDataLink.Id;
                    this.btnGetDataList.Text = @"重新获取";
                    break;
            }
        }

        /// <summary>
        /// 连接类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbType.Text.Trim()))
            {
                this.TempDataLink.LinkType = cmbType.Text;

                label5.Text = @"数据库";
                this.btnGetDataList.Visible = false;

                if (this.TempDataLink.LinkType.Equals("Oracle"))
                {
                    label5.Text = @"SID";
                }

                if (this.TempDataLink.LinkType.Equals("MySQL"))
                    this.btnGetDataList.Visible = true;

                RefreshDbLinkId();
            }
        }

        /// <summary>
        /// 根据变化更新Id
        /// </summary>
        private void RefreshDbLinkId()
        {
            if (!string.IsNullOrEmpty(this.TempDataLink.LinkType) ||
                !string.IsNullOrEmpty(this.TempDataLink.DataTable) ||
                !string.IsNullOrEmpty(this.TempDataLink.Description))
            {
                this.TempDataLink.Id = this.TempDataLink.LinkType + "_" + this.TempDataLink.DataTable + "_" +
                                        this.TempDataLink.Description;

                this.txtId.Text = this.TempDataLink.Id;
            }

        }

        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtIp_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtIp.Text.Trim()))
            {
                this.TempDataLink.Ip = txtIp.Text.Trim();
            }
        }

        /// <summary>
        /// 获取当前数据库所有列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetDataList_Click(object sender, EventArgs e)
        {
            //PubDataLinks.GetTableList(TempDataLink);
        }

        /// <summary>
        /// 端口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPort_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPort.Text.Trim()))
            {
                TempDataLink.Port = this.txtPort.Text.Trim();
            }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                TempDataLink.Name = this.txtName.Text.Trim();
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPsw_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPsw.Text.Trim()))
            {
                TempDataLink.Password = this.txtPsw.Text.Trim();
            }
        }

        /// <summary>
        /// 数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbDataBase_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbType.Text.Trim()))
            {
                TempDataLink.DataTable = this.cmbDataBase.Text.Trim();

                RefreshDbLinkId();
            }
        }

        /// <summary>
        /// 说明
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtDes_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtDes.Text.Trim()))
            {
                TempDataLink.Description = this.txtDes.Text.Trim();
                RefreshDbLinkId();
            }
        }
    }
}
